<?php
$code = <<<EOF
package dao

import (
	"code.inke.cn/bpc/facetalk/server/user/{$data["service_name"]}/model"
	"context"
	"git.inke.cn/BackendPlatform/golang/logging"
	"github.com/jinzhu/gorm"
)

//获取表名称
func (d *Dao) Get{$data['model_name']}TableName() string {
	return "{$data['table_name']}"
}

//db-slave
func (d *Dao) get{$data['model_name']}SlaveDb(ctx context.Context) *gorm.DB {
	tableName := d.Get{$data['model_name']}TableName()
	return d.db.Slave(ctx).Table(tableName)
}

//db-master
func (d *Dao) get{$data['model_name']}MasterDb(ctx context.Context) *gorm.DB {
	tableName := d.Get{$data['model_name']}TableName()
	return d.db.Master(ctx).Table(tableName)
}

func (d *Dao) Delete{$data['model_name']}ById(ctx context.Context, id int64) (err error) {
	logger := logging.For(ctx, "func", "Delete{$data['model_name']}ById", "id", id)
	db := d.get{$data['model_name']}MasterDb(ctx).Delete(model.{$data['model_name']}Model{}, "id = ?", id)
	if db.Error != nil {
		logger.Errorf("Delete{$data['model_name']}ById err: %v", err)
		return
	}
	return
}

func (d *Dao) Get{$data['model_name']}ById(ctx context.Context, id int64) (*model.{$data['model_name']}Model, error) {
	var authApply model.{$data['model_name']}Model
	err := d.get{$data['model_name']}SlaveDb(ctx).
		Where("id=?", id).
		First(&authApply).Error
	if err != nil {
		logging.For(ctx).Errorf("Get{$data['model_name']}ById ,select operation fail id:%d err:%s", id,err)
		return nil, err
	}
	return &authApply, nil
}

/**
 * @Description: 查询记录接口
 * @param ctx
 * @param where
 * @param orderBy:等于空表示按创建时间倒序
 * @param limit:大于0，小于等于0不生效
 * @return []model.{$data['model_name']}Model
 */
func (d *Dao) Find{$data['model_name']}ByCondition(ctx context.Context, where map[string]interface{}, orderBy string, limit int) []model.{$data['model_name']}Model {
	if orderBy == "" {
		orderBy = "create_time desc"
	}
	var list []model.{$data['model_name']}Model
	db := d.get{$data['model_name']}SlaveDb(ctx).Where(where)
	if limit > 0 {
		db = db.Limit(limit)
	}
	db = db.Order(orderBy).Find(&list)
	if db.Error != nil && !db.RecordNotFound() {
		logging.Errorf("Find{$data['model_name']}ByCondition  select fail , where:%d | err:%v", where, db.Error)
	}
	return list
}

/**
 * @Description: 更新数据库记录
 * @receiver d
 * @param ctx
 * @param where
 * @param updateData
 * @return error
 */
func (d *Dao) Update{$data['model_name']}ByCondition(ctx context.Context, where map[string]interface{}, updateData map[string]interface{}) error {

	db := d.get{$data['model_name']}MasterDb(ctx)
	db = db.Where(where).Update(updateData)
	if db.Error != nil {
		logging.Errorf("Update{$data['model_name']}ByCondition  Update fail , where:%+v  | updateData:%+v | err:%v", where, updateData, db.Error)
		return db.Error
	}

	return nil
}



/**
 * @Description:新增
 * @param ctx
 * @param req
 * @return record
 * @return err
 */
func (d *Dao) Add{$data['model_name']}(ctx context.Context, req model.{$data['model_name']}AddReq) (record *model.{$data['model_name']}Model, err error) {
	//todo 此处请手动赋值
	/*案例
	    record = &model.{$data['model_name']}Model{
            Id:           0,
            Uid:          req.Uid,
            TextContent:  req.TextContent,
            ImageContent: req.ImageContent,
            ClubId:       req.ClubId,
            Status:       status,
            CreateTime:   time.Now(),
            UpdateTime:   time.Now(),
        }
	*/
	db := d.get{$data['model_name']}MasterDb(ctx).
		Create(&record)
	err = db.Error

	if err != nil {
		logging.Errorf("{$data['model_name']}Add, Error:%v", err)
		return nil, err
	}
	return
}


/**
 * @Description: 查询记录接口
 * @param ctx
 * @param where
 * @param offset：从哪里开始,默认0
 * @param limit：大于0，小于等于0不生效
 * @param orderBy：等于空表示按创建时间倒序
 * @param sql:额外需要做的查询条件：例如in查询
 * @return []model.{$data['model_name']}Model
 */
func (d *Dao) Find{$data['model_name']}ByPage(ctx context.Context, where map[string]interface{},offset,limit int, orderBy string, sql string) ([]model.{$data['model_name']}Model,int) {
	//判断一些查询条件
	if orderBy == "" {
		orderBy = "create_time desc"
	}
	var list []model.{$data['model_name']}Model
	db := d.get{$data['model_name']}SlaveDb(ctx).Where(where).Offset(offset)

	if limit > 0 {
		db = db.Limit(limit)
	}

	if sql != "" {
		db = db.Where(sql)
	}

	//查询
	db = db.Order(orderBy).Find(&list)

	//总数
	totalNum := 0
	d.getUserFeedSlaveDb(ctx).Where(where).Count(&totalNum)

	if db.Error != nil && !db.RecordNotFound() {
		logging.Errorf("Find{$data['model_name']}ByCondition  select fail , where:%d | err:%v", where, db.Error)
	}
	return list,totalNum
}

EOF;
?>
<?= $code?>
